package com.fairytask.ui
{
	import com.fairytask.engine.components.BaseItem;
	import com.fairytask.engine.components.Scroll.ScrollPanel;
	import com.fairytask.engine.components.Scroll.ScrollSkin;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.comboBox.ComboBox;
	import com.fairytask.engine.components.comboBox.ComboBoxEvent;
	import com.fairytask.engine.components.group.ViewGroup;
	import com.fairytask.engine.components.text.Input;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.engine.resource.StaticIni;
	import com.jzonl.net.event.CustomToClientEvent;
	import com.jzonl.net.messages.CustomToClientEvent;
	import com.jzonl.net.messages.CustomToServerFlag;
	import com.jzonl.net.mgr.NetMgr;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.item.BagItem;
	import com.fairytask.ui.item.ResultItem;
	import com.fairytask.ui.item.SceneItem;
	import com.fairytask.ui.item.TimeShow;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.text.TextFormat;

	/**
	 *	挂机UI
	 */	
	public class OfflineBattleUI extends FrameUI
	{
		public static var instance	:OfflineBattleUI;
		
		//==============面板资源====================
		public var resultSP		:ScrollPanel;
		public var rstItemSp		:Sprite = new Sprite()
		public var offNumLab		:Clip;		//挂机次数
		public var offNumBox		:ComboBox;
		public var buyNumbtn		:Button;	//购买上限
		public var offGoldLab		:Clip;		//消耗银两
		public var offGoldIpt		:Input;
		public var needTimeLab		:Clip;
		public var timeShow		:TimeShow;
		public var buyTimeBtn		:Button;	//立即完成
		public var startBtn		:Button;	//开始
		public var stLab			:Input;		//消耗体力
		public var needSTLab		:Clip;
		//=================================
		private const TIMENUMBER	:int = 10;
		private var _cureentScene	:int;		//当前场景
		private var _curTime		:int = 1;	//当前挂机结果
		private var _infoUI		:InfoUI = InfoUI.getInstance();
		
		public static function getInstance():OfflineBattleUI
		{
			if(null	==	instance)
			{
				instance	=	new OfflineBattleUI();				
			}
			return instance;
		}
		
		public function OfflineBattleUI(popUI:Boolean=true)
		{
			super(popUI);
			init();
			initEvent();
		}
		
		/**
		 * 显示挂机界面 
		 * @param evt
		 * 
		 */
		public function showOffLine(evt:CustomToClientEvent):void
		{
			var type	:int = evt.pList.shift();
			//type==1初始化界面
			if(type == 1)
			{
				//当前场景
				_cureentScene = evt.pList.shift();
				show();
			}
			else
			{
				timeShow.startTime();
				offNumBox.canUse = false;
				buyTimeBtn.enabled = true;
				startBtn.enabled = false;
			}
		}
		
		private function init():void
		{
			//计时器
			timeShow = new TimeShow(0,"ff0000",null,1);
			timeShow.timeVal = 0;
			timeShow.x = 365;
			timeShow.y = 240;
			windowBG.addChild(timeShow);
			buyTimeBtn.enabled = false;
			
			var scrollSkin:ScrollSkin=new ScrollSkin("vScrol_1","vScrol_7");
			resultSP = new ScrollPanel(new Rectangle(0,0,335,485),rstItemSp,scrollSkin);
			resultSP.x = 25;
			resultSP.y = 30;
			windowBG.addChild(resultSP);
			resultSP.update();
			
			//初始化Combox列表数据
			for(var i:int=0;i<TIMENUMBER;i++ )
			{
				offNumBox.addItem({label:(i+1)});
			}
			
			buyNumbtn.enabled = false;
			//下拉框设置
			offNumBox.list.txtLeft();
			offNumBox.list.addHotArea();
		}
		
		private function initEvent():void
		{
			offNumBox.addEventListener(ComboBoxEvent.NXCOMBOBOX_CLICK, onNumber);
			offNumBox.addEventListener(ComboBoxEvent.COMBOBOX_CLICK, onTitle);
			//狗买次数
			buyNumbtn.addEventListener(MouseEvent.CLICK,onBuyNum);
			//立即完成
			buyTimeBtn.addEventListener(MouseEvent.CLICK,onBuyCompt);
			//开始挂机
			startBtn.addEventListener(MouseEvent.CLICK,onStart);
			//挂机信息
			model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_HOOK_CONSUME,getInfo);
			//掉落物品
			model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_DROP_ITEMS,dropItems);
			//掉落物品
			model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_COMPLETE,onTime);
			//倒计时结束
			timeShow.addEventListener(TimerEvent.TIMER_COMPLETE,onTime);
		}
		
		/**
		 * 点击下拉 
		 * @param evt
		 * 
		 */
		private function onTitle(evt:ComboBoxEvent):void
		{
			dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}
		
		/**
		 * 挂机信息
		 * @param evt
		 * 
		 */
		private function getInfo(evt:CustomToClientEvent):void
		{
			offGoldIpt.htmlStr = evt.pList.shift();
			timeShow.setTimer(false,evt.pList.shift());
			stLab.htmlStr = evt.pList.shift();
		}
		
		/**
		 * 掉落物品
		 * @param evt
		 * 
		 */
		private function dropItems(evt:CustomToClientEvent):void
		{
			while(evt.pList.length>2)
			{
				var rstItem		:ResultItem		=	new ResultItem();
				rstItem.resultNum = _curTime;
				//经验
				rstItem.getExp = evt.pList.shift();
				//灵
				rstItem.getSpt = evt.pList.shift();
				//银两
				rstItem.getSilver = evt.pList.shift();
				//掉落物品
				var amout	:int = evt.pList.shift();
				for(var i:int=0;i<amout;i++)
				{
					rstItem.getItem(i,evt.pList.shift(),evt.pList.shift());
				}
				rstItem.y = (_curTime - 1) * 160;
				rstItemSp.addChild(rstItem);
				_curTime++;
			}
			//刷新显示
			resultSP.update(100);
		}
		
		/**
		 * 挂机时间结束 
		 * @param evt
		 * 
		 */
		private function onTime(evt:*):void
		{
			timeShow.stopTime();
			resetAll();
		}
		
		/**
		 * 选择次数 
		 * @param evt
		 */
		private function onNumber(evt:ComboBoxEvent):void
		{
			setMessage(int(evt.item.label));
		}
		
		private function setMessage(val:int):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_AFK_TIMES,val);
		}
		
		/**
		 * 购买次数 
		 * @param evt
		 * 
		 */
		private function onBuyNum(evt:MouseEvent):void
		{
			
		}
		
		/**
		 * 立即完成 
		 * @param evt
		 * 
		 */
		private function onBuyCompt(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_START_AFK,1,(offNumBox.index + 1),_cureentScene);
		}
		
		/**
		 * 开始挂机
		 * @param evt
		 * 
		 */
		private function onStart(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_START_AFK,2,(offNumBox.index + 1),_cureentScene);
			clearResult();
		}
		
		/**
		 * 清除战斗结果
		 * @param sp
		 * 
		 */
		private function clearResult():void
		{
			while(rstItemSp.numChildren>0)
			{
				rstItemSp.removeChildAt(0);
			}
			resultSP.update();
			_curTime = 1;
		}
		
		/**
		 * 重置次数 
		 * 
		 */
		private function resetCombo():void
		{
			//默认为1
			offNumBox.index = 0;
			setMessage(1);
		}
		
		/**
		 * 全部重置 
		 * 
		 */
		private function resetAll():void
		{
			//立即完成
			offNumBox.canUse = true;
			buyTimeBtn.enabled = false;
			startBtn.enabled = true;
			timeShow.timeVal = 0;
			resetCombo();
		}
		
		override public function show(space:String=TOPLAYER, onshow:Function=null):void
		{
			//初始化
			clearResult();
			resetAll();
			if(WildBattleUI.getInstance().isShow)
			{
				WildBattleUI.getInstance().hide();
			}
			super.show(space,onshow);
		}
		
		/**
		 * 关闭 
		 * @param e
		 * 
		 */
		override public function hide(e:*=null):void
		{
			//立即完成可以点击（倒计时中）
			if(buyTimeBtn.enabled)
			{
				_infoUI.txtWithBtn(StaticIni.getSwfLangStr("1000000573"),InfoUI.CHOICEBTN,[onConfirm]);
			}
			else
			{
				sendHideMsg();
			}
		}
		
		/**
		 * 发送关闭信息 
		 * 
		 */
		private function sendHideMsg():void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_AFK_CANCEL);
			//停止计时
			timeShow.stopTime();
			super.hide();
		}
		
		/**
		 * 确定退出 
		 * @param data
		 * 
		 */
		private function onConfirm(data:Array=null):void
		{
			sendHideMsg();
		}
	}
}